package logic

import (
	"context"
	"encoding/json"
	"time"
	"video-hubs/model/cache"
	"video-hubs/model/mysql"

	"video-hubs/videorpc/internal/svc"
	"video-hubs/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodListLogic) GoodList(in *videorpc.GoodListRequest) (*videorpc.GoodListResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Good
	var listss []*videorpc.GetList2
	result, _ := cache.Rdb.Get(cache.Ctx, "GoodList").Result()
	if result == "" {
		var err error
		listss, err = user.GoodList(mysql.DB, listss, in)
		if err != nil {
			return nil, err
		}
		jsonData, _ := json.Marshal(listss)
		cache.Rdb.Set(cache.Ctx, "GoodList", jsonData, time.Minute*15)
	} else {
		json.Unmarshal([]byte(result), &listss)
	}

	return &videorpc.GoodListResponse{
		Listss: listss,
	}, nil
}
